using System;
using System.Collections;
using System.Data.Linq.Mapping;
using System.Runtime.Serialization;
using System.Transactions;
using Portal.Data.Helper;
using Portal.Core.Binding.FormBuilder;
using Portal.Core.Helper;
using System.Linq;

namespace Portal.Data.Entity
{
    [Table(Name = "dbo.BookOption"), Serializable, DataContractAttribute]
    public class BookOption : BaseObject
    {
        #region Properties
        [SkipBinding]
        [Column(IsPrimaryKey = true, IsDbGenerated = true)]
        public int ID { get; set; }

        [Column]
        public int Book_ID { get; set; }

        [Column]
        public int Option_ID { get; set; }

        [Column]
        public int Sort { get; set; }
        #endregion

        #region Methods
        public static BookOption FindByPrimaryKey(object objectid)
        {
            return FindByPrimaryKey<BookOption>(objectid);
        }
        public static string GetListId(IList contentOptions)
        {
            var result = "";
            foreach (BookOption item in contentOptions)
            {
                result += item.ID + ",";
            }
            if (result.Length > 0)
                result = result.Substring(0, result.Length - 1);   //remove last comma
            return result;
        }
        public static IList FindByOption(int optionId)
        {
            //getcache  -----------------------
            var parameter = new object[] { typeof(BookOption).Name, "FindByOption", optionId };
            var obj = CacheHelper.Instance.Get(parameter);
            if (obj == null)
            {
                IList result;
                //fix bug: using NOLOCK to ignore transaction deadlock
                using (var t = new TransactionScope(TransactionScopeOption.Required,
                    new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted }))
                {
                    using (var context = new CmsDataContext())
                    {
                        result = context.BookOptions.Where(o => o.Option_ID == optionId).ToList();
                        t.Complete();
                    }
                }
                CacheHelper.Instance.Set(parameter, result);
                return result;
            }
            return (IList)obj;
        }
        public static BookOption FindByGroupId(int contentId, int optionId)
        {
            //getcache  -----------------------
            var parameter = new object[] { typeof(BookOption).Name, "FindByGroupId", contentId, optionId };
            var obj = CacheHelper.Instance.Get(parameter);
            if (obj == null)
            {
                BookOption result;
                //fix bug: using NOLOCK to ignore transaction deadlock
                using (var t = new TransactionScope(TransactionScopeOption.Required,
                    new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted }))
                {
                    using (var context = new CmsDataContext())
                    {
                        result = context.BookOptions.Where(o => o.Book_ID == contentId && o.Option_ID == optionId).FirstOrDefault();
                        t.Complete();
                    }
                }
                CacheHelper.Instance.Set(parameter, result);
                return result;
            }
            return (BookOption)obj;
        }
        public static int RemoveByOption(int optId)
        {
            return LINQHelper.ExecuteNonQuery("DELETE BookOption WHERE Option_ID = {0}", optId);
        }
        #endregion Methods

    }
}
